var vm = new Vue({
    el: '#waitPayInfo', //el指定一个页面中已存在的DOM元素来挂载VUE实例
    data:{
        hotListImg:{},   //热卖的四张图片
        catalogue:{}, //目錄
        categoryNum:{
            groupid:"1"
        },
        name:"",
        WaitPayOrders:{}   //待支付订单

    },
    created:function () {
        // this.hostlist();
        // this.catalogueList();
        this.findAllWaitPayOrders();
        //加载热门商品
        var data = localStorage.getItem("hotList");
        this.hotListImg=JSON.parse(data);
        console.log(this.userByIdInfo);

        //加载目录导航条
        var data1 = localStorage.getItem("categoryNum");
        this.catalogue=JSON.parse(data1);

        var username = localStorage.getItem("userName");
        this.name=JSON.parse(username);
    },
    methods:{

        goodsdetail:function (id=1) {
            console.log(id);
            //设置缓存id。
            localStorage.setItem("thisgoodsId",JSON.stringify(id));
            this.thisgoodsId=id;
            window.location.href="/view/goodsDetail"
        },

        // hostlist:function(){
        //     //发送请求 返回最热门发布的四张图片
        //     axios.request({
        //         url:`/goods/search/hotlist`
        //     }).then(res=>{
        //         this.hotListImg=res.data;
        //         localStorage.setItem("hotList", JSON.stringify(this.hotListImg))
        //     })
        // },

        // catalogueList:function(){
        //     //发送请求 目录
        //     axios.request({
        //         url:`/categorys`
        //     }).then(res=>{
        //         this.catalogue=res.data;
        //         //将后端接口返回的数据  存到缓存中
        //         localStorage.setItem("categoryNum",JSON.stringify(res.data.data))
        //     })
        // },

        //通过目录id找 以下的商品
        findByCategory:function (category=1) {
            localStorage.setItem("categoryOne",JSON.stringify(category));
            //发送请求查询目录为1的商品

            axios.request({
                url: `/goods/searchOne/${category}`,
            }).then(res=>{
                window.location.href="/view/productList";
                localStorage.setItem("categoryNumInfo",JSON.stringify(res.data));
            });
        },
        //查看商品详情
        goodsdetail:function (id=1) {
            console.log(id);
            //设置缓存id。
            localStorage.setItem("thisgoodsId",JSON.stringify(id));
            this.thisgoodsId=id;
            axios.request({
                url:`/goods/updateVisitors/${id}/${JSON.parse(localStorage.getItem("userId"))}`
            }).then(res=>{
                window.location.href="/view/goodsDetail"
            })
        },

        /**
         * 待支付订单
         */
        findAllWaitPayOrders:function(){
            axios.request({
                url:`/userCart/findByCartIdAndWaitPay/${JSON.parse(localStorage.getItem("userId"))}`
            }).then(res=>{
                this.WaitPayOrders=res.data.data;
            })
        },
        /**
         * 删除 待支付订单
         * @param goodsid
         */
        deleteCar:function (id=1) {
            axios.request({
                url:`/userCart/deleteWaitCar/${id}/${JSON.parse(localStorage.getItem("userId"))}`
            }).then(res=>{
                layer.msg("已删除商品id为:"+id+"的购物列");
                this.findAllWaitPayOrders();
            })
        },

        /**
         * 支付訂單
         */
        pay:function(total=1,title="商品",id=1){
            layer.msg("正在提交订单总额为:"+total+"商品id为:"+id+"商品名称为:"+title);
            axios.request({
                url:`/pay/AliPay`,
                method:"post",
                headers: {
                    'Content-Type': 'application/json' //重点
                },
                data:{
                    out_trade_no:id,
                    total_amount:total,
                    subject: title
                }
            }).then(function(response){
                document.querySelector('body').innerHTML = response;
                const div = document.createElement('div');
                div.innerHTML = response.data;
                document.body.appendChild(div);
                document.forms[0].setAttribute('target', '_blank');// 新建窗口页面
                document.forms[0].submit();
            })
        },



        //注销
        logout:function () {
            layer.msg("注销倒计时......");
            sessionStorage.removeItem("userInfo");
            localStorage.clear();
            window.location.href="/view/logout"
        },

    },
    computed:{
        CaltotalPrice: function () {
            let total=0;
            for (let i = 0; i < this.WaitPayOrders.length; i++) {
                total+=this.WaitPayOrders[i].price*this.WaitPayOrders[i].goodstotal
            }
            return total;
        }
    },
    filters:{
        judge(value){
            var returnVal="";
            switch (value) {
                case "1":
                    returnVal="数码产品";
                    break;
                case "2":
                    returnVal="运动户外";
                    break;
                case "3":
                    returnVal="家居家纺";
                    break;
                case "4":
                    returnVal="家具家装";
                    break;
                case "5":
                    returnVal="生活超市";
                    break;
                case "6":
                    returnVal="唯品国际";
                    break;
                case "7":
                    returnVal="医药健康";
                    break
            }
            return returnVal;
        }
    }
});